package com.py.pythink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.pythink.bo.UserDetailBo;
import com.py.pythink.bo.UserSendBo;
import com.py.pythink.dao.UserDao;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.dao.UserSendDao;
import com.py.pythink.entity.UserDetail;
import com.py.pythink.entity.UserSend;
import com.py.pythink.service.intf.UserSendService;
import com.py.pythink.third.BaiduCensorUtil;
import com.py.pythink.util.BeanUtil;
import com.py.pythink.vo.R;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class UserSendServiceImpl extends ServiceImpl<UserSendDao, UserSend> implements UserSendService {
    @Autowired
    private UserSendDao dao;
    @Autowired
    private UserDetailDao userDetailDao;

    @Override
    public R insert(UserSendBo bo, int uid) {
        System.err.println(BaiduCensorUtil.textCensor(bo.getInfo()));
        if (BaiduCensorUtil.textCensor(bo.getInfo())) {
            UserSend userSend = BeanUtil.copyBean(UserSend.class, bo);
            userSend.setUid(uid);
            UserDetail userDetail = userDetailDao.selectByUid(uid);
            if (userDetail.getNickname() == null) {
                return R.fail("亲，先完成个人信息哦！");
            }
            userSend.setNickname(userDetail.getNickname());
            if (dao.insertt(userSend) > 0) {
                return R.ok();
            }
        }
        return R.fail("发帖失败，请检查网络");
    }

    @Override
    public R selectByUid(int uid) {
        return R.ok(dao.selectByUid(uid));
    }

    @Override
    public R delete(int uid, int id) {
        if (dao.delete(uid, id) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R usersendDetail(int id) {
        return R.ok(dao.usersendDetail(id));
    }

    @Override
    public R djok(int uid, int id) {
        return null;
    }

    @Override
    public R getHouse() {
        List<UserSend> userSends = dao.selectList(null);//查询所有信息，此信息是没有评论的
        List<UserSend> person = dao.person();//查询所有有过评论的帖子带评论

        for (UserSend us : userSends) {
            for (UserSend pp : person) {
                if (us.getId() == pp.getId()) {
                    us.setChildren(pp.getChildren());
                }
            }
        }
        return R.ok(userSends);

    }


}
